﻿using System;
using System.Collections.Generic;
using VideoStore.Domain.Entities;
using System.Linq;

namespace VideoStore.Domain.DAL
{
    public interface ICatalogueDataAccess
    {
        /// <summary>
        /// Gets the video by id.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        Catalogue GetVideoById(int id);

        /// <summary>
        /// Gets the video by catalogue number.
        /// </summary>
        /// <param name="number">The number.</param>
        /// <returns></returns>
        Catalogue GetVideoByCatalogueNumber(string number);

        /// <summary>
        /// Gets the videos by director.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        IEnumerable<Catalogue> GetVideosByDirector(string name);

        /// <summary>
        /// Creates the video.
        /// </summary>
        /// <param name="newVideo">The new video.</param>
        /// <returns></returns>
        Catalogue CreateVideo(Catalogue newVideo);

        /// <summary>
        /// Gets the videos by genre.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        IEnumerable<Catalogue> GetVideosByGenre(string name);
    }

    public class CatalogueDataAccess : BaseDataAccess, ICatalogueDataAccess
    {
        #region Implementation of ICatalogueDataAccess

        /// <summary>
        /// Gets the video by id.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        public Catalogue GetVideoById(int id)
        {
            Catalogue video = DataContext.Catalogues.SingleOrDefault(c => c.id == id);
            return video;
        }

        /// <summary>
        /// Gets the video by catalogue number.
        /// </summary>
        /// <param name="number">The number.</param>
        /// <returns></returns>
        public Catalogue GetVideoByCatalogueNumber(string number)
        {
            Catalogue video = DataContext.Catalogues.SingleOrDefault(c => c.catalogue_no == number);
            return video;
        }

        /// <summary>
        /// Gets the videos by director.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public IEnumerable<Catalogue> GetVideosByDirector(string name)
        {
            var videos = DataContext.Catalogues.Where(c => c.director.Contains(name)).AsEnumerable();
            return videos;
        }

        /// <summary>
        /// Creates the video.
        /// </summary>
        /// <param name="newVideo">The new video.</param>
        /// <returns></returns>
        public Catalogue CreateVideo(Catalogue newVideo)
        {
            DataContext.Catalogues.InsertOnSubmit(newVideo);
            DataContext.SubmitChanges();
            return newVideo;
        }

        /// <summary>
        /// Gets the videos by genre.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public IEnumerable<Catalogue> GetVideosByGenre(string name)
        {
            IEnumerable<Catalogue> videos = DataContext.Catalogues.Where(c => c.genre == name).AsEnumerable();
            return videos;
        }

        #endregion
    }
}
